﻿using System.Linq;
using System.Reflection;
using System.Text;
using Properties;

namespace Borg
{
    public class ParamValue : RepositoryMetaElement
    {
        internal static int nextId = 1;

        internal static QualificationState unqualifiedState =
            QualificationState.QualificationMissing |
            QualificationState.ParamValueAlreadyExists |
            QualificationState.ParamValueEmptyName;

        internal PlugTypeInfo plugTypeInfo;
        internal readonly ParamValueAttribute paramValueAttribute;
        internal bool defaultValue;

        public override string Name
        {
            get { return paramValueAttribute.Name; }
        }

        public override Assembly Assembly
        {
            get { return plugTypeInfo.extensionTypeInfo.pluginInfo.assembly; }
        }

        public object Value
        {
            get { return paramValueAttribute.Value; }
        }

        public override bool IsQualified
        {
            get { return (qualificationState & unqualifiedState) == QualificationState.None; }
        }

        static ParamValue()
        { }

        internal ParamValue(PlugTypeInfo plugTypeInfo, ParamValueAttribute paramValueAttribute)
        {
            id = nextId++;
            this.plugTypeInfo = plugTypeInfo;
            this.paramValueAttribute = paramValueAttribute;
        }

        internal ParamValue(PlugTypeInfo plugTypeInfo, ParamValueAttribute paramValueAttribute, bool defaultValue)
            : this(plugTypeInfo, paramValueAttribute)
        {
            this.defaultValue = defaultValue;
        }

        internal override QualificationState InternalQualify()
        {
            var builder = new StringBuilder();

            if (string.IsNullOrEmpty(Name))
            {
                builder.AppendFormat("\n  {0}", Messages.ParameterNameEmpty);
                var paramValue = this;
                var status = (long)(paramValue.qualificationState | QualificationState.ParamValueEmptyName);
                paramValue.qualificationState = (QualificationState)status;
            }

            if (plugTypeInfo.paramValues.Any(paramVal => paramVal != this &&
                paramVal.Name == Name && paramVal.IsQualified))
            {
                builder.AppendFormat("\n  {0}", Messages.ParamValueAlreadyExists);
                var paramValue = this;
                var status = (long)(paramValue.qualificationState | QualificationState.ParamValueAlreadyExists);
                paramValue.qualificationState = (QualificationState)status;
            }

            var parmVal = this;
            var stat = (long)(parmVal.qualificationState & ~QualificationState.QualificationMissing);
            parmVal.qualificationState = (QualificationState)stat;
            RuntimeBase.LogQualifyResult(
                this,
                qualificationState,
                unqualifiedState,
                QualificationState.None,
                Messages.QualifyParamValue,
                builder.ToString());

            return qualificationState;
        }
    }
}

